package cn.moondev.node;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import cn.moondev.node.NodeChainHandler;
import cn.moondev.node.exception.NodeException;

@Component
public class NodeManager implements ApplicationContextAware {
	
	private ApplicationContext context;

	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context = context;
	}

	public <K,V> void run(String chainId,Map<K,V> params) {
		NodeChainHandler chain = findNodeChainById(chainId);
		chain.run(params);
	}
	
	public NodeChainHandler findNodeChainById(String chainId) {
		Object obj = context.getBean(chainId);
		if(!(obj instanceof NodeChainHandler)) {
			throw new NodeException("Cannot find a node chain by: " + chainId);
		}
		return (NodeChainHandler)obj;
	}
}
